package org.dong.samples.dao.Impl;

import java.util.ArrayList;
import java.util.List;
import org.dong.samples.dao.TestHibernateDAO;
import org.dong.samples.model.ExamTest;
import org.dong.samples.model.ExamTestType;
import org.dong.samples.model.PagerModel;
import org.dong.samples.model.Subject;
import org.dong.samples.model.SystemContext;
import org.dong.samples.model.vo.ExamTestVO;
import org.dong.samples.service.TestHibernateService;
import org.dong.samples.service.UserSubjectHibernateService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class TestHibernateDAOImpl extends BaseHibernateDAO<ExamTest> implements TestHibernateDAO  {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Autowired
	private UserSubjectHibernateService userSubjectHiberanteService;
	
	@Autowired
	private TestHibernateService testHiberanteService;
	
	public Session getSession(){
		return this.sessionFactory.getCurrentSession();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<ExamTest> findAllTest() throws Exception{
		Session session = getSession();
		String hql = "FROM ExamTest";
		List<ExamTest> userList = session.createQuery(hql).list();
		return userList;
	}
	
	@Override
	public ExamTest getTestById(Integer id){
		Session session = getSession();
		return (ExamTest) session.get(ExamTest.class, id);
	}
	
	@Override
	public void createTest(ExamTest testHiberante) throws Exception{
		Session session = getSession();
		session.save(testHiberante);
	}
	
	@Override
	public void editTest(ExamTest testHiberante) throws Exception{
		Session session = getSession();
		session.update(testHiberante);
	}
	
	@Override
	public void deleteTest(Integer id) throws Exception{
		Session session = getSession();
		ExamTest testHiberante = (ExamTest) session.get(ExamTest.class, id);
		if(testHiberante != null){
			session.delete(testHiberante);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExamTestType> findExamTestType() throws Exception {
		Session session = getSession();
		String hql = "FROM ExamTestType";
		List<ExamTestType> typeList = session.createQuery(hql).list();
		return typeList;
	}
	
	public ExamTestType findByExamTestTypeId(Integer typeId) throws Exception{
		Session session = getSession();
		return (ExamTestType) session.get(ExamTestType.class, typeId);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<ExamTestVO> pageFindExamTest() throws Exception {
		Session session = getSession();
		PagerModel<ExamTestVO> page = new PagerModel<ExamTestVO>();
		List<ExamTestVO> list = new ArrayList<ExamTestVO>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		//使用hibernate完成分页
		List<ExamTest> tempDates = session.createQuery("FROM ExamTest").setFirstResult(offset).setMaxResults(pageSize).list();
		for(ExamTest test : tempDates){
			ExamTestVO testVO = new ExamTestVO();
			BeanUtils.copyProperties(test, testVO);
			Subject subject = userSubjectHiberanteService.getSubjectById(test.getSubject().getSubId());
			testVO.setSubName(subject.getSubName());
			ExamTestType type = testHiberanteService.findByExamTestTypeId(test.getExamTestType().getId());
			testVO.setTestType(type.getTestType());
			testVO.setTestTypeScore(type.getTestTypeScore());
			list.add(testVO);
		}
		page.setDates(list);
		page.setOffset(offset);
		page.setPageSize(pageSize);
		
		//获取总记录数
		long totalSize = (long)this.getSession().createQuery("select count(*) from ExamTest").uniqueResult();
		page.setTotalSize(totalSize);
		return page;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<ExamTest> getTestListBySubAndTestType(Integer subId, Integer id) {
		StringBuilder hql = new StringBuilder();
		hql.append(" SELECT et.id as id, et.content as content, et.choose_a as chooseA, et.choose_b as chooseB, et.choose_c as chooseC, et.choose_d as chooseD, et.answer as answer, et.sub_id "); 
		hql.append(" FROM exam_test et,exam_subject es,exam_test_type ett ");
		hql.append(" WHERE et.sub_id=es.sub_id ");
		hql.append(" AND et.test_type_id=ett.id ");
		hql.append(" AND et.sub_id=:subId ");
		hql.append(" AND ett.id=:id ");
		List<ExamTest> test = getSession().createSQLQuery(hql.toString())
				.addScalar("id", IntegerType.INSTANCE)
				.addScalar("content", StringType.INSTANCE)
				.addScalar("chooseA", StringType.INSTANCE)
				.addScalar("chooseB", StringType.INSTANCE)
				.addScalar("chooseC", StringType.INSTANCE)
				.addScalar("chooseD", StringType.INSTANCE)
				.addScalar("answer", StringType.INSTANCE)
				.setResultTransformer(Transformers.aliasToBean(ExamTest.class))
				.setInteger("subId", subId)
				.setInteger("id", id)
				.list();
		return test;
	}

	@Override
	public ExamTest findAnwer(Integer testId) {
		
		return null;
	}

	
	
}
